package com.mf.basic.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mf.basic.services.ServiceUser;
import com.mf.basic.entity.DtoUser;
import com.mf.basic.log_api.LogApi;
import com.mf.basic.rs.ResponseResult;
import com.mf.basic.services.ServiceUserRole;
import com.mf.basic.utils.MyAssert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Created by Luke on 2025/3/7 16:58.
 */
@RestController
@ResponseResult
@SaCheckLogin
@RequestMapping("/userRole")
public class ControllerUserRole implements MyAssert {

    @Resource
    private ServiceUserRole serviceUserRole;
    @Resource
    private ServiceUser serviceUser;

    /**
     * 添加用户角色关联(覆盖)
     * @param params roleId,userIds
     */
    @LogApi("角色-管理用户")
    @SaCheckPermission("userRole.add")
    @PostMapping("/addByCover")
    public void doAddByCover(@RequestBody Map<String, Object> params) {
        Long roleId = (long) idIsNull(params.get("roleId"), "角色ID不能为空");
        String userIds = strIsNull(params.get("userIds"), "用户IDs不能为空");
        serviceUserRole.doAddByCover(roleId, userIds);
    }

    /**
     * 添加用户角色关联(追加)
     * @param params roleId,userIds
     */
    @LogApi("角色-管理用户")
    @SaCheckPermission("userRole.add")
    @PostMapping("/addByAppend")
    public void doAddByAppend(@RequestBody Map<String, Object> params) {
        Long roleId = (long) idIsNull(params.get("roleId"), "角色ID不能为空");
        String userIds = strIsNull(params.get("userIds"), "用户IDs不能为空");
        serviceUserRole.doAddByAppend(roleId, userIds);
    }

    /**
     * 添加用户角色关联(追加)
     * @param params roleId,userIds
     */
    @LogApi("角色-删除用户")
    @SaCheckPermission("userRole.add")
    @PostMapping("/delete")
    public void doDelete(@RequestBody Map<String, Object> params) {
        Long roleId = (long) idIsNull(params.get("roleId"), "角色ID不能为空");
        String userIds = strIsNull(params.get("userIds"), "用户IDs不能为空");
        serviceUserRole.doDelete(roleId, userIds);
    }

    /**
     * 根据角色ID分页查询用户
     * @param params roleId,pageNum,pageSize
     * @return IPage<DtoUser>
     */
    @SaCheckPermission("user.page")
    @PostMapping("/pageUserByRole")
    public IPage<DtoUser> findByRole(@RequestBody Map<String, Object> params) {
        pageIsNull(params);
        idIsNull(params.get("roleId"), "角色ID不能为空");
        return serviceUser.findPageByParams(params);
    }

}
